#!/bin/ash -efu

ini_config_set_test1() { # UnitTest
	. ../shell-ini-config
	local value="Aaa Bbb"

	cp -f ini-config/z.ini ini-config/ini_config_set-t1.ini

	ini_config_set ini-config/ini_config_set-t1.ini global user "$value"

	local rc=0
	cmp -s ini-config/ini_config_set-t1.ini ini-config/ini_config_set-t1.ini.expect || rc=$?

	[ "$rc" != 0 ] ||
		rm -f  -- ini-config/ini_config_set-t1.ini

	assertTrue $rc
}

ini_config_set_test2() { # UnitTest
	. ../shell-ini-config
	local value='$1 \n\1 "Bbb"'

	cp -f ini-config/z.ini ini-config/ini_config_set-t2.ini

	ini_config_set ini-config/ini_config_set-t2.ini global user "$value"

	local rc=0
	cmp -s ini-config/ini_config_set-t2.ini ini-config/ini_config_set-t2.ini.expect || rc=$?

	[ "$rc" != 0 ] ||
		rm -f  -- ini-config/ini_config_set-t2.ini

	assertTrue $rc
}

ini_config_set_test3() { # UnitTest
	. ../shell-ini-config
	local value='XXX'

	cp -f ini-config/z.ini ini-config/ini_config_set-t3.ini

	ini_config_del ini-config/ini_config_set-t3.ini global user
	ini_config_set ini-config/ini_config_set-t3.ini global user "$value"

	local rc=0
	cmp -s ini-config/ini_config_set-t3.ini ini-config/ini_config_set-t3.ini.expect || rc=$?

	[ "$rc" != 0 ] ||
		rm -f  -- ini-config/ini_config_set-t3.ini

	assertTrue $rc
}

ini_config_set_test4() { # UnitTest
	. ../shell-ini-config
	local value='XXX'

	cp -f ini-config/z.ini ini-config/ini_config_set-t4.ini

	ini_config_comment ini-config/ini_config_set-t4.ini global user
	ini_config_set ini-config/ini_config_set-t4.ini global user "$value"

	local rc=0
	cmp -s ini-config/ini_config_set-t4.ini ini-config/ini_config_set-t4.ini.expect || rc=$?

	[ "$rc" != 0 ] ||
		rm -f  -- ini-config/ini_config_set-t4.ini

	assertTrue $rc
}

ini_config_set_test5() { # UnitTest
	. ../shell-ini-config
	local value='ZZZ'

	cp -f ini-config/z.ini ini-config/ini_config_set-t5.ini

	ini_config_set ini-config/ini_config_set-t5.ini sect1 opt "$value"

	local rc=0
	cmp -s ini-config/ini_config_set-t5.ini ini-config/ini_config_set-t5.ini.expect || rc=$?

	[ "$rc" != 0 ] ||
		rm -f  -- ini-config/ini_config_set-t5.ini

	assertTrue $rc
}

ini_config_set_test6() { # UnitTest
	. ../shell-ini-config
	cp -f ini-config/krb5.conf ini-config/ini_config_set-t6.ini

	ini_config_set ini-config/ini_config_set-t6.ini libdefaults default_realm SCHOOL.ALT

	local rc=0
	cmp -s ini-config/ini_config_set-t6.ini ini-config/ini_config_set-t6.ini.expect || rc=$?

	[ "$rc" != 0 ] ||
		rm -f  -- ini-config/ini_config_set-t6.ini

	assertTrue $rc
}

ini_config_set_test7() { # UnitTest
	. ../shell-ini-config
	local rc=0 result expect="$PROG: No such file or directory: ini-config/not-exist"
	result="$(ini_config_set ini-config/not-exist sect1 opt "xxx" 2>&1)" || rc=$?
	assertEquals "Wrong return code: rc=$rc" "$rc" "1"
	assertEquals "$result" "$result" "$expect"
}

ini_config_set_test8() { # UnitTest
	. ../shell-ini-config
	cp -f ini-config/krb5.include.conf ini-config/ini_config_set-t8.ini

	ini_config_set ini-config/ini_config_set-t8.ini libdefaults default_realm SCHOOL.ALT

	local rc=0
	cmp -s ini-config/ini_config_set-t8.ini ini-config/ini_config_set-t8.ini.expect || rc=$?

	[ "$rc" != 0 ] ||
		rm -f  -- ini-config/ini_config_set-t8.ini

	assertTrue $rc
}
